import grails.util.Environment

import com.pijunn.common.*
import com.pijunn.common.enums.*
import com.pijunn.label.Label
import com.pijunn.label.LabeledItem
import com.pijunn.security.*
import com.pijunn.shipping.ShippingRate
import com.pijunn.shipping.ShippingWeight
import com.pijunn.users.*


class BootStrap {
	
    def init = { servletContext ->
		
		switch(Environment.current){
	      case Environment.DEVELOPMENT:
			def userRole = SecRole.findByAuthority('ROLE_USER') ?: new SecRole(authority: 'ROLE_USER').save(failOnError: true)
			def adminRole = SecRole.findByAuthority('ROLE_ADMIN') ?: new SecRole(authority: 'ROLE_ADMIN').save(failOnError: true)
			
			def address = new MailingAddress(address1: '1316 Durango Ridge Rd', address2: '', city : 'Durango', state: 'CO', zipCode: '81301', country: 'US')
	
			def adminUser = AppUser.findByUsername('admin') ?: new AppUser(
	                username: 'admin',
					firstName: 'App', lastName: 'User',
					contactEMail: 'mrosent@gmail.com',
					contactCellPhone: '', contactOtherPhone: '', contactSkypeId: '',
					mailingAddress: address,
	                password: 'landshark', 
					createTime: new Date(),
	                enabled: true).save(failOnError: true)
	 
			if (!adminUser.authorities.contains(adminRole)) {
				SecUserSecRole.create adminUser, adminRole
			}
			// Set up test data
			def labelInstance = Label.findBySerialNumber(10000000) ?: {
				for (def serial in 10000000..10000099) {
					def li = new Label(serialNumber:serial, creationDate: new Date(), type: LabelType.Small, createdByUserName:adminUser.username, registeredTo: serial<10000002 ? adminUser : null, assigned: serial < 10000002).save()
					if (li.hasErrors())
						log.error(li.errors.each {println(it)})
				}
				def assignedLabels = Label.findAllByAssigned(true)
				def descriptions = [10000000:[name:'Marks Mac', description:'Macbook Pro 17 inch'],10000001:[name:'Pates iPad',description:'iPad 3 with leather case']]
				for (Label li in assignedLabels) {
					def lii = new LabeledItem(name:descriptions.get(li.serialNumber).get('name'), description:descriptions.get(li.serialNumber).get('description'), label: li)
					lii.save()
					if (lii.hasErrors())
						log.error(lii.errors.each {println(it)})
				}
			}()
	      break
	
	      case Environment.PRODUCTION:
	      break
	    }
	
		// Set up shipping methods
		def shippingList = ShippingRate.getAll()
		if (!shippingList || shippingList.size() == 0) {
			shippingList = [new ShippingRate(packageType:'FedEx Envelope', dimensions:new Dimensions(height:12, width:9.5, depth:1),emptyWeight:0.25),
				new ShippingRate(packageType:'FedEx Pak/Box', dimensions:new Dimensions(height:19.5, width:15, depth:3.5),emptyWeight:1.1)]
			
			for (shippingRate in shippingList) {
				shippingRate.save(failOnError: true)
			}
		}
		// And default item weights
		def shippingWeights = ShippingWeight.getAll()
		if (!shippingWeights || shippingWeights.size() == 0) {
			shippingWeights = [
				new ShippingWeight(name:'iphone', estimatedWeight:1),
				new ShippingWeight(name:'phone', estimatedWeight:1),
				new ShippingWeight(name:'android', estimatedWeight:1),
				new ShippingWeight(name:'ipad', estimatedWeight:2),
				new ShippingWeight(name:'ipad', estimatedWeight:2),
				new ShippingWeight(name:'tablet', estimatedWeight:2),
				new ShippingWeight(name:'kindle', estimatedWeight:2),
				new ShippingWeight(name:'nook', estimatedWeight:2),
				new ShippingWeight(name:'reader', estimatedWeight:2),
				new ShippingWeight(name:'laptop', estimatedWeight:8),
				new ShippingWeight(name:'computer', estimatedWeight:8),
				new ShippingWeight(name:'macbook', estimatedWeight:6),
				new ShippingWeight(name:'keys', estimatedWeight:2),
				new ShippingWeight(name:'camera', estimatedWeight:1)
			]
			
			for (shippingWeight in shippingWeights) {
				shippingWeight.save(failOnError: true)
			}
		}
    }
    def destroy = {
    }
}
